package com.example.demo.service;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.bean.SCustomer;
import com.example.demo.bean.SEmp;
import com.example.demo.bean.SRegion;
import com.example.demo.common.exception.DataValidException;
import com.example.demo.common.utils.Response;
import com.example.demo.mapper.SCustomerMapper;
import com.example.demo.mapper.SEmpMapper;
import com.example.demo.mapper.SOrdMapper;
import com.example.demo.mapper.SRegionMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;



@Service
public class SCustomerService {
	@Autowired
	private SCustomerMapper scusMapper;
	@Autowired 
	private SRegionMapper srMapper;
	@Autowired
	private SEmpMapper seMapper;
	@Autowired
	private SOrdMapper soMapper;
	//搜索所有的顾客信息
	public PageInfo<SCustomer> findAllByPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum,pageSize);
		//执行查找行为
		List<SCustomer> list=scusMapper.selectAll();
		if (list==null) {
			list=new ArrayList<SCustomer>(0);
		}
		return new PageInfo<SCustomer>(list);//将查找结果返回
	}
	

	
	
	
	
	//按条件搜索顾客信息

	public PageInfo<SCustomer> findByName(String name, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum,pageSize);
		name=(name==null||"".equals(name))?"":"%"+name+"%";
		List<SCustomer> list= scusMapper.selectByName(name);
		if (list==null) {
			list=new ArrayList<>(0);
			System.out.println("====================================================");
		}
		
		return new PageInfo<>(list);
	}
	
	//添加或修改顾客信息
	public void saveOrUpdate(SCustomer sc) {
		//判断输入的信息是否存在
		if (sc.getRegionId()==null||sc.getSalesRepId()==null) {
			//判断两个外键是否存在
			throw new DataValidException("请填写完整信息");
		}else {
			SRegion sr=srMapper.selectByPrimaryKey(sc.getRegionId());
			SEmp se=seMapper.selectByPrimaryKey(sc.getSalesRepId());
			if (sr==null||se==null) {
				throw new DataValidException("外键信息不存在");
			}
		}
		if (sc.getId()==null) {
			scusMapper.insert(sc);
		}else {
			scusMapper.updateByPrimaryKey(sc);
		}
	}
	
	//删除顾客信息
	//customer作为ord的外键
	//判断customer下属是否有ord的存在
	//不存在删除customer
	
	public int deleteById(Integer id) {
		//判断id是否为空
		if (id==null) {
			throw new DataValidException("请输入顾客id");
		}
		//按照顾客id查找信息，如果找不到就提示删除失败
		SCustomer sc=scusMapper.selectByPrimaryKey(id);
		if (sc==null) {
			throw new DataValidException("删除失败");
		}
		//判断下属是否存在ord的信息,如果不存在就进行下一步操作
		if (!soMapper.selectByCustomerId(sc.getId()).isEmpty()) {
			throw new DataValidException("要删除的信息下面存在订单信息，请删除对应的订单信息后在进行该客户的删除");
		}
		scusMapper.deleteByPrimaryKey(id);
		return 0;
	}
	//批量删除顾客信息
	//customer作为ord的外键
	//判断customer下属是否有ord的存在
	//不存在删除customer
	public int batchDeleteById(int [] id) {
		//判断是否为空
		if (id.length==0||id==null) {
			throw new DataValidException("请选择一批id");
		}
		//查找要删除的顾客信息
		for(int i:id) {
			SCustomer sCustomer=scusMapper.selectByPrimaryKey(i);
			if (sCustomer==null) {
				throw new DataValidException("删除失败");
			}
			//判断下属是否存在订单信息
			if (!soMapper.selectByCustomerId(sCustomer.getId()).isEmpty()) {
				throw new DataValidException("要删除的信息下面存在订单信息，请删除对应的订单信息后在进行该客户的删除");
			}
		}
		//删除顾客信息
		scusMapper.batchDelete(id);
		return 0;
	}
	
}
